﻿using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Hosting;

namespace GuideAPI
{
    public class ContosoChatHub : Hub
    {
        public async Task NewContosoChatMessage(string name, string message)
        {
            //await Clients.All.addNewMessageToPage(name, message);
            
            //use a string variable as the method name
            string methodToCall = "addNewMessageToPage";
            IClientProxy proxy = Clients.All;
            await proxy.Invoke(methodToCall, name, message);

            //HTTP header data
            INameValueCollection headers = Context.Headers;
            //Query string data
            string parameterValue = Context.QueryString["parameterName"];

            HttpContextBase httpContext = Context.Request.GetHttpContext();

            string userName = Clients.CallerState.userName;
            string computerName = Clients.CallerState.computerName;
        }

        public async Task JoinGroup(string groupName)
        {
            await Groups.Add(Context.ConnectionId, groupName);
            await Clients.Group(groupName).addContosoChatMessageToPage(Context.ConnectionId + " added to group");
        }

        public Task LeaveGroup(string groupName)
        {
            //there is no api for getting a group membership list or a list of groups
            return Groups.Remove(Context.ConnectionId, groupName);
        }

        public override Task OnConnected()
        {
            var version = Context.QueryString["version"];
            if (version != "1.0")
            {
                Clients.Caller.notifyWrongVersion();
            }

            // Add your own code here.
            // For example: in a chat application, record the association between
            // the current connection ID and user name, and mark the user as online.
            // After the code in this method completes, the client is informed that
            // the connection is established; for example, in a JavaScript client,
            // the start().done callback is executed.
            return base.OnConnected();
        }

        

        public override Task OnDisconnected(bool stopCalled)
        {
            // Add your own code here.
            // For example: in a chat application, mark the user as offline, 
            // delete the association between the current connection id and user name.
            return base.OnDisconnected(stopCalled);
        }

        public override Task OnReconnected()
        {
            // Add your own code here.
            // For example: in a chat application, you might have marked the
            // user as offline after a period of inactivity; in that case 
            // mark the user as online again.
            return base.OnReconnected();
        }
    }
}